package mx.tarhimurhu.amoxcalli.util;


import mx.tarhimurhu.amoxcalli.Project;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;


public class DBConnectionHelper 
{
	
	private DBConnectionHelper(){}
	
	
	public static void write(ArrayList<Project> list) throws Exception
	{
		// Write to file
		FileOutputStream fos = new FileOutputStream("list.ser");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		System.out.println("Writing object to file...");
		oos.writeObject(list);
		oos.close();
	}

	
	@SuppressWarnings("unchecked")
	public static ArrayList<Project> read() throws Exception
	{
		ArrayList<Project> list;
		File file = new File("list.ser");

		if(file.exists())
		{
			// Read from file
			FileInputStream fis = new FileInputStream("list.ser");
			ObjectInputStream ois = new ObjectInputStream(fis);
			System.out.println("Reading object...");
			list = (ArrayList<Project>) ois.readObject();
			ois.close();
		}
		else
		{
			file.createNewFile();
			list = new ArrayList<Project>();
		}
		
		return list;
	}

}
